﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace TeamWPF
{
    /// <summary>
    /// Interaction logic for FreightWindow.xaml
    /// </summary>
    public partial class CompanyWindow : Window
    {
        private bool isWindowClosing = false;

        private CourierManager.CompanyCustomer senderCompany;
        private CourierManager.Customer recepient;
        private bool canProcceed;
        public CompanyWindow()
        {
            InitializeComponent();
        }

        public void OnCloseCompanyWindow(object sender, CancelEventArgs e)
        {
            isWindowClosing = true;
        }

        private void ProcesssCompanyStatute(object sender, RoutedEventArgs e)
        {
            if (sender as MenuItem == NewClient)
            {
                DefaultStatute.Header = NewClient.Header;
            }
            if (sender as MenuItem == VIPClient)
            {
                DefaultStatute.Header = VIPClient.Header;
            }
            if (sender as MenuItem == ClientWithPreferences)
            {
                DefaultStatute.Header = ClientWithPreferences.Header;
            }
        }

        private void CompanyIDNumberLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                CourierManager.ValidatorIDNumber.ValidateCompanyIDNumber(this.CompanyIDNumberInput.Text);
                CourierManager.CompanyCustomer tempCustomer =
               new CourierManager.CompanyCustomer(ulong.Parse(this.CompanyIDNumberInput.Text));
                tempCustomer.PathToDatabase = "../../../CourierManager/CompanyCustomers.txt";
                List<string> allCompanyClients = tempCustomer.ReadClientData();
                for (int record = 0; record < allCompanyClients.Count; record++)
                {
                    string[] dataArray = allCompanyClients[record].Split('*');
                    string currentIDNumber = dataArray[3];
                    if (this.CompanyIDNumberInput.Text == currentIDNumber)
                    {
                        this.CompanyNameInput.Text = dataArray[1];
                        this.CompanyAddressInput.Text = dataArray[2];
                        this.CompanyPhoneInput.Text = dataArray[4];
                        break;
                    }
                }
            }

            catch (CourierManager.InvalidIDNumberException ide)
            {
                if (!isWindowClosing)
                {
                    this.CompanyIDNumberInput.Focusable = true;
                    Keyboard.Focus(this.CompanyIDNumberInput);
                    ErrorWindow newIDWindow = new ErrorWindow();
                    newIDWindow.ErrorText.Text = ide.Message;
                    newIDWindow.Show();
                }
            }
        }

        private void ProcessCompanyClient(object sender, RoutedEventArgs e)
        {
            if (this.SenderGrid.Visibility == System.Windows.Visibility.Visible)
            {
                if (!(String.IsNullOrWhiteSpace(this.CompanyNameInput.Text) || String.IsNullOrWhiteSpace(this.CompanyAddressInput.Text)
                    || String.IsNullOrWhiteSpace(this.CompanyIDNumberInput.Text) || String.IsNullOrWhiteSpace(this.CompanyPhoneInput.Text)))
                {
                    canProcceed = true;
                }
            }
            else if (this.IndividualRecipientGrid.Visibility == System.Windows.Visibility.Visible)
            {
                if (!((String.IsNullOrWhiteSpace(this.IndividualRecipientFirstNameInput.Text) 
                    || String.IsNullOrWhiteSpace(this.IndividualRecipientLastNameInput.Text)
                    || String.IsNullOrWhiteSpace(this.IndividualRecipientAddressInput.Text)
                    || String.IsNullOrWhiteSpace(this.IndividualRecipientPhoneInput.Text))))
                {
                    canProcceed = true;
                }
            }
            else if (this.CompanyRecipientGrid.Visibility == System.Windows.Visibility.Visible)
            {
                if (!((String.IsNullOrWhiteSpace(this.CompanyRecipientNameInput.Text) 
                    || String.IsNullOrWhiteSpace(this.CompanyRecipientAddressInput.Text)
                    || String.IsNullOrWhiteSpace(this.CompanyRecipientPhoneInput.Text))))
                {
                    canProcceed = true;
                }
            }
            else if (this.FreightGrid.Visibility == System.Windows.Visibility.Visible)
            {
                if (!(String.IsNullOrWhiteSpace(this.FreightWeightInput.Text)))
                {
                    canProcceed = true;
                }
            }
                //collapse the sender form and move on to the recepient form
            if (canProcceed == true)
            {
                if (this.SenderGrid.Visibility == System.Windows.Visibility.Visible)
                {
                    //set bool variable canProcceed to false,so it can be used in the next window
                    canProcceed = false;
                    ulong phone = ulong.Parse(CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(this.CompanyPhoneInput.Text));
                    ulong idNumber = ulong.Parse(this.CompanyIDNumberInput.Text);
                    senderCompany = new CourierManager.CompanyCustomer(this.CompanyNameInput.Text, this.CompanyAddressInput.Text,
                        idNumber, phone);
                    this.CompanyRecipientGrid.Visibility = System.Windows.Visibility.Visible;
                    this.RecipientType.Visibility = System.Windows.Visibility.Visible;
                    this.RecipientTypeMenu.Visibility = System.Windows.Visibility.Visible;
                    this.CompanyForm.Visibility = System.Windows.Visibility.Collapsed;
                    this.SenderGrid.Visibility = System.Windows.Visibility.Collapsed;
                }

                else if (this.SenderGrid.Visibility == System.Windows.Visibility.Collapsed
                    && (this.CompanyRecipientGrid.Visibility == System.Windows.Visibility.Visible ||
                    this.IndividualRecipientGrid.Visibility == System.Windows.Visibility.Visible))
                {
                    if (this.RecipientMenuHeader.Header == IndividualRecipient.Header)
                    {
                        ulong phone = ulong.Parse(CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(this.IndividualRecipientPhoneInput.Text));
                        recepient = new CourierManager.IndividualCustomer(this.IndividualRecipientFirstNameInput.Text, this.IndividualRecipientLastNameInput.Text,
                            this.IndividualRecipientAddressInput.Text, 0, phone);
                    }
                    else //(this.RecipientMenuHeader.Header == CompanyRecipient.Header)
                    {
                        ulong phone = ulong.Parse(CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(this.CompanyRecipientPhoneInput.Text));
                        recepient = new CourierManager.CompanyCustomer(this.CompanyRecipientNameInput.Text, this.CompanyRecipientAddressInput.Text, 0,
                            phone);
                    }
                    canProcceed = false;
                    this.IndividualRecipientGrid.Visibility = System.Windows.Visibility.Collapsed;
                    this.CompanyRecipientGrid.Visibility = System.Windows.Visibility.Collapsed;
                    this.RecipientType.Visibility = System.Windows.Visibility.Collapsed;
                    this.RecipientTypeMenu.Visibility = System.Windows.Visibility.Collapsed;
                    this.FreightGrid.Visibility = System.Windows.Visibility.Visible;
                    this.SubmitCompanyData.Visibility = System.Windows.Visibility.Collapsed;
                } 
            }
            else
            {
                ErrorWindow emptyInputFieldsWindow = new ErrorWindow();
                emptyInputFieldsWindow.ErrorText.Text = "Empty input fields!";
                emptyInputFieldsWindow.Show();
            }
        }

        private void ProccessFreightData(object sender, RoutedEventArgs e)
        {
            if ((!String.IsNullOrWhiteSpace(this.FreightWeightInput.Text)))
            {
                canProcceed = true;
            }
            if (canProcceed == true)
            {
                CourierManager.Freight currentFreight;
                decimal weight = decimal.Parse(this.FreightWeightInput.Text);
                CourierManager.Location location = (CourierManager.Location)Enum.Parse(typeof(CourierManager.Location), this.comboboxLocation.SelectedValue.ToString());

                if (HasFilledDimensionsInput(this.FreightWidthInput.Text, this.FreightHeightInput.Text, this.FreightDepthInput.Text) == true)
                {
                    decimal width = decimal.Parse(this.FreightWidthInput.Text);
                    decimal height = decimal.Parse(this.FreightHeightInput.Text);
                    decimal depth = decimal.Parse(this.FreightDepthInput.Text);
                    currentFreight = new CourierManager.Freight(weight, senderCompany, recepient, width, height, depth, location);
                    MessageBox.Show("Freight submitted sucessfully!");
                    this.Close();
                }
                else
                {
                    currentFreight = new CourierManager.Freight(weight, senderCompany, recepient, location);
                    MessageBox.Show("Freight submitted sucessfully!");
                    this.Close();
                }
                currentFreight.PathToDatabase = @"../../../CourierManager/ListOfBillOfLadings.txt";
                currentFreight.WriteInDatabase(); 
            }
            else
            {
                ErrorWindow emptyInputFieldsWindow = new ErrorWindow();
                emptyInputFieldsWindow.ErrorText.Text = "Empty weight input field!";
                emptyInputFieldsWindow.Show();
            }
        }

        private void ChangeRecipientType(object sender, RoutedEventArgs e)
        {

            if (sender as MenuItem == IndividualRecipient)
            {

                this.RecipientMenuHeader.Header = IndividualRecipient.Header;
                //make company recepient fields collapse
                this.CompanyRecipientGrid.Visibility = System.Windows.Visibility.Collapsed;
                //make individual recepient fields visible
                this.IndividualRecipientGrid.Visibility = System.Windows.Visibility.Visible;
                
            }


            if (sender as MenuItem == CompanyRecipient)
            {
                this.RecipientMenuHeader.Header = CompanyRecipient.Header;
                //make individual recepient fields collapse
                this.IndividualRecipientGrid.Visibility = System.Windows.Visibility.Collapsed;
                //make company fields visible
                this.CompanyRecipientGrid.Visibility = System.Windows.Visibility.Visible; 
            }
            
        }
        private bool HasFilledDimensionsInput(string width, string height, string depth)
        {
            if (String.IsNullOrEmpty(width) || String.IsNullOrEmpty(height) || String.IsNullOrEmpty(depth))
            {
                return false;
            }
            return true;
        }

        private void CompanyNameLostFocus(object sender, RoutedEventArgs e)
        {
                try
                {
                    string companyName = this.CompanyNameInput.Text;
                    CourierManager.ValidatorName.CompanyNameValidator(companyName);
                }
                catch (CourierManager.InvalidNameException ex)
                {
                    if (!isWindowClosing)
                    {
                        this.CompanyNameInput.Focusable = true;
                        Keyboard.Focus(this.CompanyNameInput);
                        ErrorWindow errorWindow = new ErrorWindow();
                        errorWindow.ErrorText.Text = ex.Message;
                        errorWindow.Show();
                    }
                } 
        }

        private void CompanyPhoneLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string companyPhone = this.CompanyPhoneInput.Text;
                companyPhone = CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(companyPhone);
            }
            catch (CourierManager.InvalidPhoneNumberException phoneNumberEx)
            {
                if (!isWindowClosing)
                {
                    this.CompanyPhoneInput.Focusable = true;
                    Keyboard.Focus(this.CompanyPhoneInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = phoneNumberEx.Message;
                    errorWindow.Show();
                }
            }
        }
        private void IndividualRecepientFirstNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string firstName = this.IndividualRecipientFirstNameInput.Text;
                CourierManager.ValidatorName.PersonalFirstNameValidator(firstName);
            }
            catch (CourierManager.InvalidNameException ex)
            {
                if (!isWindowClosing)
                {
                    this.IndividualRecipientFirstNameInput.Focusable = true;
                    Keyboard.Focus(this.IndividualRecipientFirstNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = ex.Message;
                    errorWindow.Show();
                }
            }
        }
       
        private void IndividualRecipientLastNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string lastName = this.IndividualRecipientLastNameInput.Text;
                CourierManager.ValidatorName.PersonalFirstNameValidator(lastName);
            }
            catch (CourierManager.InvalidNameException ex)
            {
                if (!isWindowClosing)
                {
                    this.IndividualRecipientLastNameInput.Focusable = true;
                    Keyboard.Focus(this.IndividualRecipientLastNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = ex.Message;
                    errorWindow.Show();
                }
            }
        }

        private void IndividualRecipientPhoneLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string recepientPhone = this.IndividualRecipientPhoneInput.Text;
                CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(recepientPhone);
            }
            catch (CourierManager.InvalidPhoneNumberException phoneNumberEx)
            {
                if (!isWindowClosing)
                {
                    this.IndividualRecipientPhoneInput.Focusable = true;
                    Keyboard.Focus(this.IndividualRecipientPhoneInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = phoneNumberEx.Message;
                    errorWindow.Show(); 
                }
            }
        }

        private void CompanyRecipientNameLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string companyName = this.CompanyRecipientNameInput.Text;
                CourierManager.ValidatorName.CompanyNameValidator(companyName);
            }
            catch (CourierManager.InvalidNameException nameEx)
            {
                if (!isWindowClosing)
                {
                    this.CompanyRecipientNameInput.Focusable = true;
                    Keyboard.Focus(this.CompanyRecipientNameInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = nameEx.Message;
                    errorWindow.Show(); 
                }
            }
        }

        private void CompanyRecipientPhoneLostFocus(object sender, RoutedEventArgs e)
        {
            try
            {
                string recepientPhone = this.CompanyRecipientPhoneInput.Text;
                CourierManager.ValidatorPhone.ValidatePhoneNumberFromString(recepientPhone);
            }
            catch (CourierManager.InvalidPhoneNumberException phoneNumberEx)
            {
                if (!isWindowClosing)
                {
                    this.CompanyRecipientPhoneInput.Focusable = true;
                    Keyboard.Focus(this.CompanyRecipientPhoneInput);
                    ErrorWindow errorWindow = new ErrorWindow();
                    errorWindow.ErrorText.Text = phoneNumberEx.Message;
                    errorWindow.Show(); 
                }
            }
        }
        
    }
}
